package com.tuling.learnmongodb.aop;

import com.alibaba.fastjson.JSONObject;
import com.tuling.learnmongodb.common.ResponseModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionAdvice {

	/**
     * 拦截捕捉自定义异常 MethodArgumentNotValidException.class
     * JSR-303 校验失败处理
     *
     * @param ex
     * @return JSR-303
     */
    @ResponseBody
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public ResponseModel<Object> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException ex) {
        List<FieldError> allErrors = ex.getBindingResult().getFieldErrors();
        if (log.isInfoEnabled()) {
            @SuppressWarnings("unchecked")
            Map<String, Object>[] errors = new Map[allErrors.size()];
            for (int i = 0; i < errors.length; i++) {
                FieldError err = allErrors.get(i);
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("name", (Object) err.getField());
                map.put("value", err.getRejectedValue());
                map.put("name", err.getDefaultMessage());
                errors[i] = map;
            }
            log.info("参数校验错误:{}", JSONObject.toJSONString(errors));
        }
        FieldError fieldError = allErrors.get(0);
        String msg = fieldError.getField() + ": " + fieldError.getDefaultMessage();
        return new ResponseModel<>("00000001", msg);
    }


    @ResponseBody
    @ExceptionHandler(value = RuntimeException.class)
    public ResponseModel<Object> runtimeExceptionHandler(RuntimeException ex) {
        log.info("runtimeExceptionHandler errorHandler....");
        log.error(ex.getMessage(), ex);
        return new ResponseModel<>("00000001", "发生异常");
    }

    /**
     * 全局异常捕捉处理
     *
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
    public ResponseModel<Object> httpRequestMethodNotSupportedExceptionHandler(HttpRequestMethodNotSupportedException ex) {
    	log.info("HttpRequestMethodNotSupportedException....");
        log.error(ex.getMessage());
        return new ResponseModel<>("00000001", ex.getMessage());
    }
    
    /**
     * 全局异常捕捉处理
     *
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public ResponseModel<Object> exceptionHandler(Exception ex) {
        log.info("errorHandler exception....");
        log.error(ex.getMessage(), ex);
        return new ResponseModel<>("00000001", "发生异常");
    }

    /**
     * 全局异常捕捉处理
     *
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Throwable.class)
    public ResponseModel<Object> errorHandler(Throwable ex) {
        log.info("exceptionHandler Throwable....");
        log.error(ex.getMessage(), ex);
        return new ResponseModel<>("00000001", "发生异常");
    }
}
